const app = getApp();
import Poster from '../../../wxa-plugin-canvas/miniprogram_dist/poster/poster';

function promisify(fn) {
  return function(obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function(res) {
        resolve(res)
      }

      obj.fail = function(res) {
        reject(res)
      }
      fn(obj)
    })
  }
}
Page({

  /**
   * 异步生成海报
   */
  onCreatePoster() {
    // setData配置数据
    this.setData({posterConfig: this.data.posterConfig}, () => {
      Poster.create(true, this);
    });
  },

  onPosterSuccess(e) {
    const { detail } = e;
    console.log(detail);
    this.setData({
      poster_url: detail
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    resource: {},
    poster: '',
    canvasId: 'poster',
    qrcode: "",
    banner: "",
    logo: "",
    wechat:{},


    // 海报参数
    posterConfig: null,
    poster_url: null,

    //画布参数
    canvasParam:{
      x:0,
      y:0,
      width: 750,
      height: 1000,
      bgColor:'#fff',
      fontSize:14,
      fontColor:'#000',
    }
  },
  create() {
    this.setData({
      posterConfig: {
        width: 800,
        height: 1200,
        backgroundColor: "#FFF",
        images: [
          {
            x: 0,
            y: 0,
            url: this.data.resource.banner,
            width: 800,
            height: 800
          },
          {
            x: 70,
            y: 810,
            zIndex: 800,
            url: this.data.resource.logo,
            width: 240,
            height: 190
          },
          {
            x: 450,
            y: 810,
            url: this.data.resource.qrcode,
            width: 300,
            height: 300
          }
        ],
        texts: [
          {
            x: 210,
            y: 1010,
            text: this.data.wechat.nickName,
            lineHeight: 50,
            fontSize: 40,
            lineNum: 1,
            width: 450,
            zIndex: 1000,
            textAlign: 'center'
          },
          {
            x: 210,
            y: 1060,
            text: '邀请您注册悠家会员',
            lineHeight: 30,
            fontSize: 30,
            lineNum: 1,
            width: 450,
            textAlign: 'center'
          },
          {
            x: 210,
            y: 1100,
            text: '新人注册可得' + this.data.resource.register.lower_limit + '元立减券',
            lineHeight: 30,
            fontSize: 30,
            lineNum: 1,
            width: 450,
            textAlign: 'center'
          }
        ]
      }
    });
    wx.hideLoading()
    // 生成海报
    this.onCreatePoster();
  },

  getPoster() {
    let _this = this;
    app.HttpsRequest('share/getPoster').then(ret => {
      if (ret) {
        _this.setData({
          resource: ret.data
        });
        this.create()
      }
    })
  },
  savePhoto() {
    let _this = this;
    app.loading('正在保存...');
    setTimeout(() => {
      wx.saveImageToPhotosAlbum({
        filePath: _this.data.poster_url,
        success(res) {
          app.tips('保存成功');
        },
        fail(err) {
          app.tips('保存失败，请刷新页面重试');
        }
      })
    }, 2500)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.loading();
    this.getPoster();
    this.setData({
      wechat:JSON.parse(wx.getStorageSync('wechat'))
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})